<template>
    <view>
        <view class="box">
            <view class="textg"><text>已添加物品</text></view>
            <view class="usergoods">
                <addgood v-for="(item, index) in data.wp" :index="index" :wp="item" :key="item" @wpDelete="wpDelete"
                    @showEdit="showEdit(item, index)"></addgood>
            </view>
            <view class="add_product">
                <button @click="isShowadd_pro">
                    <image src="/static/img/myhome/add@3x.png"></image>
                    <text>继续添加</text>
                </button>
            </view>
        </view>
        <!-- 展示继续添加商品弹窗 s -->
        <u-popup v-model="isShow" mode="bottom">
            <addprods :prevWeight="aWeight" :prevTime="bTime" @myEven="change" @closewin="colse" ref="popupAdd">
            </addprods>
        </u-popup>
        <!-- 展示编辑商品弹窗 s -->
        <u-popup v-model="isShowEdit" mode="bottom">
            <editgood @myEven="changeEdit" @closewin="colseEdit" ref="popupEdit"></editgood>
        </u-popup>
        <view class="footer">
            <view style="display: flex;justify-content: space-between;align-items: center;">
                <view>
                    <view style="line-height: 40rpx;font-size: 30rpx;font-weight: bold;">{{title}}</view>
                </view>
                <view class="sub">
                    <button @click="post"><i>提交</i></button>
                </view>
            </view>
        </view>
    </view>
</template>

<script>
    import addprods from './add_product.vue';
    import editgood from './edit_product.vue';
    import Addgood from './goods_item.vue';
    export default {
        components: {
            addprods,
            editgood,
            Addgood,
        },
        data() {
            return {
                userInfo: null,
                isShow: false,
                isShowEdit: false,
                isShowMobile: false,
                data: {
                    wp: []
                },
                buttonseletct: '',
                vuserList: [],
                isHsType: 1,
                selectShow: false,
                selectText: null,
                aWeight: '',
                password: '',
                popupShow: false,
                is_edit_price: 0,
                handle_type: 1,
                bTime: null,
                house_id: 1,
                defaultType: '其他过磅',
                title: '',
                to_service_id: 0,
                receipt_list: [],
                transfer_id: 0
            };
        },
        onLoad(e) {
            if (e.transfer_id) {
                this.transfer_id = e.transfer_id
            }
            uni.setNavigationBarTitle({
                title: '内部调拨'
            });
            this._getInfo()
            this._getServiceReceiptInfo()
        },
        computed: {
            // 计算累计金额
            amount() {
                let total = 0;
                if (this.$u.test.isEmpty(this.data.wp)) {
                    return;
                }
                this.data.wp.forEach((item, index) => {
                    if (item.total_price > 0) {
                        total += Number(item.total_price);
                    }
                });
                return total.toFixed(2);
            }
        },
        methods: {
            // 用户信息api
            _getInfo() {
                this.$u.api.getInfo().then(res => {
                    this.userInfo = res;
                    this.is_edit_price = res.is_edit_price
                    this._getwptypelist(res.service_id)
                })
            },
            _getServiceReceiptInfo() {
                this.$u.api.getServiceReceiptInfo({
                    transfer_id: this.transfer_id
                }).then(res => {
                    this.data.wp = res
                })
            },
            _getwptypelist(service_id) {
                this.$u.api.getwptypelist({
                    service_id: service_id
                }).then(res => {
                    res.forEach((item, index) => {
                        item.plist.forEach((it, idx) => {
                            return it.extra = it.u_price + ',' + it.unit
                        })
                    })
                    uni.setStorageSync('setWptypeList', res)
                })
            },
            post() {
                var that = this;
                if (that.$u.test.isEmpty(that.data.wp)) {
                    uni.showToast({
                        title: '请添加至少一项物品',
                        duration: 1000,
                        icon: 'none'
                    });
                    return;
                }
                that.toReceive();
            },
            // 弹窗
            isShowadd_pro() {
                this.isShow = true;
                if (this.isHsType == 1) {
                    this.$refs.popupAdd.handPriceDisabled();
                    this.$refs.popupAdd.prevWeight(this.aWeight);
                    this.$refs.popupAdd.prevTime(this.bTime);
                    //20221118
                    // this.$refs.popupAdd.getTweight();
                    let PriceDisabled = this.is_edit_price == 1 ? false : true
                    this.$refs.popupAdd.setPriceDisabled(PriceDisabled);
                }
            },
            // 将子组件的数据 push 进 数组中
            change(options) {
                this.aWeight = options.a_weigh
                this.bTime = options.a_time
                this.data.wp.push(options);
                this.isShow = false;
            },
            colse() {
                this.isShow = false;
            },
            showEdit(item, index) {
                let isGetWeight = 1; //是否获取重量
                this.$refs.popupEdit.cre(item, index, isGetWeight);
                this.isShowEdit = true;
                let PriceDisabled = this.is_edit_price == 1 ? false : true

                this.$refs.popupEdit.setPriceDisabled(PriceDisabled);
            },
            wpDelete(index) {
                this.data.wp.splice(index, 1);
                this.aWeight = this.data.wp[this.data.wp.length - 1].a_weigh
                this.bTime = this.data.wp[this.data.wp.length - 1].a_time
            },
            // 将子组件的数据 push 进 数组中
            changeEdit(item, index) {
                this.data.wp[index] = item;
                this.isShowEdit = false;
            },
            colseEdit() {
                this.isShowEdit = false;
            },
            toReceive(id) {
                this.$u.api.toReceiveReceipt({
                        id: this.transfer_id,
                        wp: this.data.wp
                    }).then(res => {
                        uni.showToast({
                            title: '接收成功',
                            icon: 'none'
                        })
                        setTimeout(() => {
                            uni.navigateBack({
                                delta: 2
                            });
                        }, 2500)
                    })
                    .catch(err => {
                        uni.showToast({
                            title: err.msg,
                            duration: 2000,
                            icon: 'none'
                        });
                    });
            }
        }
    };
</script>

<style lang="scss" scoped>
    .cover_view {
        height: 300rpx;
        background-color: pink;
    }

    .box {
        padding: 30rpx;
        // background-color: #eee;
        // min-height: 1500rpx;
    }

    .textg {
        font-size: 32rpx;
        line-height: 38rpx;
        color: #2d3945;
        margin: 30rpx 0 30rpx 30rpx;
    }

    span {
        font-size: 28rpx;
        line-height: 48rpx;
        color: #666666;
        margin-left: 30rpx;
    }

    .userphone {
        background-color: #ffffff;
        border-radius: 20rpx;
        padding-left: 30rpx;
        line-height: 98rpx;
        margin-bottom: 30rpx;

        button {
            background-color: #2b85e4;
            color: #ffffff;
            transform: scale(0.8);
        }

        view:nth-child(1) {
            border-bottom: 1px solid #eeeeee;
            flex-grow: 1;
        }

        view {
            display: flex;
            align-items: center;

            input {
                padding-left: 30rpx;
            }
        }
    }

    .useraddress {
        height: 196rpx;
        background-color: #ffffff;
        border-radius: 20rpx;
        padding: 0 30rpx;
        line-height: 98rpx;
        margin-bottom: 30rpx;

        view:nth-child(1) {
            border-bottom: 1px solid #eeeeee;
        }

        view {
            display: flex;
            align-items: center;

            input {
                padding-left: 30rpx;
            }
        }

        image {
            float: right;
            margin-top: 30rpx;
            width: 50rpx;
            height: 50rpx;
        }
    }

    .usertime {
        height: 308rpx;
        background-color: #ffffff;
        border-radius: 20rpx;
        padding: 0 30rpx;
        line-height: 98rpx;
        margin-bottom: 30rpx;

        picker {
            font-size: 28rpx;
            color: #666666;
            text-align: left;
            margin-left: 30rpx;
            flex: 1;
        }

        view:nth-child(1) {
            border-bottom: 1px solid #eeeeee;
            display: flex;
        }

        view:nth-child(2) {
            margin-top: 30rpx;
            display: flex;
            flex-wrap: wrap;
        }

        button {
            width: 190rpx;
            height: 70rpx;
            border-radius: 35rpx;
            background-color: #ffffff;
            border: solid 2rpx #f2f2f2;
            margin: 0 10rpx 10rpx 0;
        }

        i {
            font-size: 24rpx;
            line-height: 70rpx;
            color: #999999;
        }

        .selectButoom {
            background-color: #1bc078;
            // color: #ffffff;

            i {
                color: #ffffff;
            }
        }
    }

    .add_product {
        margin-bottom: 10vh;

        button {
            width: 570rpx;
            height: 98rpx;
            line-height: 98rpx;
            background-color: #ffffff;
            border-radius: 49rpx;
        }

        text {
            font-size: 28rpx;
            color: #1bc078;
        }

        image {
            margin-right: 20rpx;
            width: 24rpx;
            height: 24rpx;
        }
    }

    .footer {
        position: fixed;
        bottom: 0;
        left: 0;
        width: 100%;
        padding: 10rpx 30rpx;
        background-color: #ffffff;
        padding-bottom: env(safe-area-inset-bottom);
        display: flex;
        flex-direction: column;

        text {
            font-size: 32rpx;
            color: #191919;
        }

        span {
            font-size: 32rpx;
            color: #ff4200;
            margin: 0 28rpx;
        }

        >button {
            position: absolute;
            left: 2%;
            top: -100%;
            border-radius: 999rpx;
            background-color: #f0ad4e;
            color: #fff;
            padding: 20rpx 30rpx;
        }

        .sub {
            display: flex;
            justify-content: space-around;
            align-items: center;
            margin-top: 5rpx;
            margin-bottom: 5rpx;

            button {
                width: 262rpx;
                height: 78rpx;
                background-color: #1bc078;
                border-radius: 39rpx;
                margin: 0;

                i {
                    font-size: 32rpx;
                    color: #ffffff;
                }
            }
        }
    }

    .showeditgood {
        height: 100%;
        position: fixed;
        left: 0;
        right: 0;
        top: 0;
        z-index: 998;
    }

    .boxson {
        padding: 30rpx;
        font-size: 24rpx;
        color: #888888;
        background-color: #Fff;
        margin-bottom: 20rpx;
    }

    .hr {
        margin: 18rpx 0;
        height: 1rpx;
        background-color: #efeff0;
    }

    .main {
        position: relative;

        .img {
            width: 50px;
            height: 50px;
            position: absolute;
            bottom: 0;
            right: 0;
        }
    }

    .image {
        position: relative;
        top: 5rpx;
        left: 0rpx;
        width: 26rpx;
        height: 32rpx;
    }

    .down {
        width: 50rpx;
        height: 50rpx;
    }

    .call {
        position: absolute;
        right: 40rpx;
        height: 40rpx;
        background-color: #defbef;
        border-radius: 20rpx;
        font-size: 24rpx;
        color: #1bc078;
        line-height: 40rpx;
    }

    .setbutton {
        // position: absolute;
        // right: 30rpx;
        width: 163rpx;
        height: 60rpx;
        background-color: #ff4200;
        border-radius: 30rpx;
        line-height: 60rpx;
        color: #FFFFFF;
        font-size: 24rpx;
        margin: unset;
    }

    .editsetbutton {
        // position: absolute;
        // right: 200rpx;
        min-width: 163rpx;
        height: 60rpx;
        background-color: #ffffff;
        border-radius: 30rpx;
        border: solid 2rpx #0babe0;
        font-size: 24rpx;
        line-height: 60rpx;
        color: #0babe0;
    }

    .oksetbutton {
        // position: absolute;
        // right: 30rpx;
        min-width: 163rpx;
        height: 60rpx;
        background-color: #0babe0;
        border-radius: 30rpx;
        font-size: 24rpx;
        line-height: 60rpx;
        letter-spacing: 0rpx;
        color: #ffffff;
        margin-left: 30rpx;
    }

    p {
        margin-top: 20rpx;
    }

    .textget {
        float: right;
        color: #FF0000;
    }

    .textblue {
        color: #0babe0;
    }

    .texteee {
        color: #888888;
    }

    .datasum {
        // position: absolute;
        // right: 30rpx;
        // top: 0;
        margin-right: 40rpx;
    }

    .button {
        // margin-bottom: 30rpx;
    }
</style>
